//
//  ByViewSweepTable.m
//  mijifen_ios
//
//  Created by mc on 2017/10/25.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByViewSweepTable.h"
// cell
#import "ByCellRedPriceOrCount.h"
#import "ByCellRedConditionOrValid.h"
#import "ByCellRedNote.h"

@interface ByViewSweepTable()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, copy)   NSArray     *titleArray;
@property (nonatomic, copy)   NSArray     *subArray;
@property (nonatomic, copy)   NSArray     *midArray;

@property (nonatomic, strong) NSMutableDictionary *infoDic;         // 用来存放信息数组
@end

@implementation ByViewSweepTable
- (void)setupUI{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = BY_TABLE_COLOR;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self addSubview:self.tableView];
}

- (void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    self.tableView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
}

#pragma mark - getter
- (NSArray *)titleArray{
    if (!_titleArray) {
        _titleArray = @[@"红包金额：",@"红包个数：",@"使用条件：",@"有效期：",@"备注："];
    }
    return _titleArray;
}

- (NSArray *)subArray{
    if (!_subArray) {
        _subArray = @[@"¥",@"个",@"以上可用",@"天可用",@""];
    }
    return _subArray;
}

- (NSArray *)midArray{
    if (!_midArray) {
        _midArray = @[@"支付",@"领取后"];
    }
    return _midArray;
}

- (NSMutableDictionary *)infoDic{
    if (!_infoDic) {
        _infoDic = [NSMutableDictionary dictionary];
    }
    return _infoDic;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    __weak typeof(self) weakSelf = self;
    if (indexPath.section < 2) {
        ByCellRedPriceOrCount *cell = [ByCellRedPriceOrCount cellWithTableView:tableView andTitle:self.titleArray[indexPath.section] andSubTitle:self.subArray[indexPath.section]];
        cell.priceOrCountBlock = ^(NSString *priceOrCount) {
            NSLog(@"%@",priceOrCount);
            [weakSelf.infoDic setValue:priceOrCount forKey:[NSString stringWithFormat:@"%ld",indexPath.section + 10]];
            weakSelf.editBlock([weakSelf.infoDic copy]);
        };
        return cell;
    }else if(indexPath.section < 4){
        ByCellRedConditionOrValid *cell = [ByCellRedConditionOrValid cellWithTableView:tableView andTitle:self.titleArray[indexPath.section] andSecTitle:self.midArray[indexPath.section - 2] andThrTitle:self.subArray[indexPath.section]];
        cell.conditionBlock = ^(NSString *condition) {
            NSLog(@"%@",condition);
            [weakSelf.infoDic setValue:condition forKey:[NSString stringWithFormat:@"%ld",indexPath.section + 10]];
            weakSelf.editBlock([weakSelf.infoDic copy]);
        };
        return cell;
    }else{
        ByCellRedNote *cell = [ByCellRedNote cellWithTableView:tableView];
        cell.redNoteBlock = ^(NSString *node) {
            NSLog(@"%@",node);
            [weakSelf.infoDic setValue:node forKey:[NSString stringWithFormat:@"%ld",indexPath.section + 10]];
            weakSelf.editBlock([weakSelf.infoDic copy]);
        };
        return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        UIView *headerView = [[UIView alloc] init];
        UILabel *headerLabel = [[UILabel alloc] init];
        [headerView addSubview:headerLabel];
        NSString *headerStr = @"席卷整个平台所有会员，\n只要你敢付出，就一定有收货！";
        CGFloat height = [headerStr getRectOfStringWithSize:CGSizeMake(SCREEN_WIDTH, MAXFLOAT) font:16].size.height + 10;
        [headerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(headerView.mas_top).offset(10);
            make.left.equalTo(headerView.mas_left);
            make.size.mas_offset(CGSizeMake(SCREEN_WIDTH, height));
        }];
        headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, height + 20);
        headerLabel.text = headerStr;
        headerLabel.textAlignment = NSTextAlignmentCenter;
        headerLabel.textColor = [UIColor lightGrayColor];
        headerLabel.font = FONT(16);
        headerLabel.numberOfLines = 2;
        return headerView;
    }
    return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return [@"席卷整个平台所有会员，\n只要你敢付出，就一定有收货！" getRectOfStringWithSize:CGSizeMake(SCREEN_WIDTH, MAXFLOAT) font:16].size.height + 30;
    }
    return 0.0001;
}


@end
